package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.product.api.domain.SkuPrice;
import com.spzx.product.api.domain.vo.ProductSkuVo;
import com.spzx.product.api.domain.vo.SkuStockVo;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.service.IProductDetailsService;
import com.spzx.product.service.IProductService;
import com.spzx.product.service.IProductSkuService;
import com.spzx.product.service.ISkuStockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author lee
 * @since 2024-07-15
 */
@Tag(name = "商品管理")
@RestController
@RequestMapping("/product")
@RequiresLogin
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private ISkuStockService skuStockService;

    @Autowired
    private IProductDetailsService productDetailsService;

    @Operation(summary = "分页查询商品")
    @GetMapping("list")
    public TableDataInfo list(Product product){
        startPage();
        List<Product> list = productService.getProductList(product);
        return getDataTable(list);
    }

    @Operation(summary = "商品审核")
    @Log(title = "商品管理", businessType = BusinessType.EXAMINE)
    @GetMapping("updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus){
        productService.updateAuditStatus(id,auditStatus);
        return success();
    }

    @Operation(summary = "商品上下架")
    @GetMapping("updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Long id, @PathVariable Integer status){
        productService.updateStatus(id,status);
        return success();
    }

    @Operation(summary = "新增商品")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addProduct(@Validated @RequestBody Product product){
    return toAjax(productService.addProduct(product));
    }

    @Operation(summary = "获取商品详情")
    @GetMapping("{id}")
    public AjaxResult getDetailById(@PathVariable Integer id){
        return success(productService.getDetailById(id));
    }

    @Operation(summary = "修改商品")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateProduct(@Validated @RequestBody Product Product){
        return toAjax(productService.updateProduct(Product));
    }

    @Operation(summary = "删除商品")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("{id}")
    public AjaxResult deleteProduct(@PathVariable("id") List<Long> ids){
        return toAjax(productService.removeProduct(ids));
    }

    @Operation(summary = "内部调用-获取商品sku的基本信息")
    @InnerAuth
    @GetMapping("/productSku/{skuId}")
    public R<ProductSku> getProductSku(@PathVariable("skuId") Integer skuId){
        ProductSku productSku = productSkuService.getProductSku(skuId);
        return R.ok(productSku);
    }

    @Operation(summary = "内部调用-获取商品详情")
    @InnerAuth
    @GetMapping("/product/{id}")
    public R<Product> getProduct(@PathVariable("id") Integer id){
        return R.ok(productService.getProductById(id)) ;
    }

    @Operation(summary = "内部调用-获取sku价格")
    @InnerAuth
    @GetMapping("/skuPrice/{skuId}")
    public R<SkuPrice> getSkuPrice(@PathVariable("skuId") Integer skuId){
        SkuPrice skuPrice = productSkuService.getSkuPrice(skuId);
        return R.ok(skuPrice);
    }

    @Operation(summary = "内部调用-根据skuId获取sku价格")
    @InnerAuth
    @GetMapping("/skuPriceList")
    public R<List<SkuPrice>> getSkuPrice(@RequestParam("skuIdList")List<Long> skuIdList){
        List<SkuPrice> skuPriceList = productSkuService.getSkuPriceList(skuIdList);
        return R.ok(skuPriceList);
    }


    @Operation(summary = "内部调用-获取sku的库存和销量")
    @InnerAuth
    @GetMapping("/skuStock/{skuId}")
    public R<SkuStockVo> getSkuStock(@PathVariable("skuId") Integer skuId){
        return R.ok(skuStockService.getSkuStock(skuId));
    }

    @Operation(summary = "内部调用-根据productId获取sku的规格值")
    @InnerAuth
    @GetMapping("/skuSpecValue/{id}")
    public R<Map<String, Long>> getSkuSpecValue(@PathVariable("id") Long id)
    {
        Map<String, Long> skuSpecValue = productService.getSkuSpecValue(id);
        return R.ok(skuSpecValue);
    }

    @Operation(summary = "内部调用-根据productId获取商品详细信息")
    @InnerAuth
    @GetMapping(value = "/getProductDetails/{id}")
    public R<List<String>> getProductDetails(@PathVariable("id") Long id)
    {
        return R.ok(productDetailsService.getProductDetails(id));
    }


}
